package com.yycwxt.modules.admin.core_flows;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.Flows;
import com.yycwxt.domain.Store;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.Map;


/**
 * Created by Mr.Zhang on 2022/8/26 21:42
 */


@RestController
@Api("收入支出流水接口文档")
@Validated
@RequestMapping("/flows")
public class FlowsController {

    private final FlowsService flowsService;



    public FlowsController(@Autowired FlowsService flowsService) {
        this.flowsService = flowsService;
    }

    @GetMapping("/{accountDate}")
    @ApiOperation("根据账期查询全部流水")
    public ReturnResult selectAllByAccountDate(@ApiParam(name = "accountDate", required = true, type = "String") @PathVariable Integer accountDate) {
        return ReturnResult.buildSuccessResult(flowsService.selectAllByAccountDate(accountDate));
    }

    @GetMapping("/accountDate")
    @ApiOperation("查询全部账期")
    public ReturnResult selectAccountDate() {
        return ReturnResult.buildSuccessResult(flowsService.selectAccountDate());
    }

    @GetMapping()
    @ApiOperation("查询全部流水")
    public ReturnResult selectAll() {
        return ReturnResult.buildSuccessResult(flowsService.selectAll());
    }

    @GetMapping("/input")
    @ApiOperation("根据店铺编码或仓库编码查询")
    public ReturnResult selectByInput(@ApiParam(name = "input", required = true, type = "String") @Validated String input){
        return ReturnResult.buildSuccessResult(flowsService.selectByInput(input));
    }

    @PostMapping()
    @ApiOperation(("新增流水"))
    public ReturnResult add(@RequestBody Flows flows) {
        System.out.println(flows);
        try {
            return ReturnResult.buildSuccessResult(flowsService.add(flows.getOrderNumber(), flows.getAccountDate(), flows.getShopCode(),
                    flows.getStoreCode(), flows.getSubjectCode(), flows.getSubjectDirection(), flows.getCost(), flows.getRemark()));
        } catch (Exception e) {
            return ReturnResult.buildFailureResult(e.toString());
        }
    }

    @GetMapping("/subjectName")
    @ApiOperation("查询全部科目名称")
    public ReturnResult selectSubjectName() {
        return ReturnResult.buildSuccessResult(flowsService.selectSubjectName());
    }

    @GetMapping("/shopName")
    @ApiOperation("查询全部科目名称")
    public ReturnResult selectShopName() {
        return ReturnResult.buildSuccessResult(flowsService.selectShopName());
    }

    @GetMapping("/storeName")
    @ApiOperation("查询全部仓库名称")
    public ReturnResult selectStoreName() {
        return ReturnResult.buildSuccessResult(flowsService.selectStoreName());
    }















    //@DeleteMapping("/{id}")
    //@ApiOperation("根据id删除相应的流水")
    //public ReturnResult deleteById(@ApiParam(name = "id", required = true, type = "Integer") @PathVariable Integer id) {
    //    try {
    //        if (flowsServiceTest.removeById(id)) {
    //            return ReturnResult.buildSuccessResult(true);
    //        } else {
    //            return ReturnResult.buildFailureResult("id可能不存在", false);
    //        }
    //    } catch (Exception e) {
    //        return ReturnResult.buildFailureResult("id必须为数字", false);
    //    }
    //}

    //@PutMapping()
    //@ApiOperation("根据id修改相应流水")
    //public ReturnResult updateById(@ApiParam(name = "Flows对象", required = true, type = "Flows") @RequestBody @Validated Flows flows) {
    //    try {
    //        if (flowsServiceTest.updateById(flows)) {
    //            return ReturnResult.buildSuccessResult(true);
    //        } else {
    //            return ReturnResult.buildFailureResult("id不存在", false);
    //        }
    //    //} catch (DuplicateKeyException dke) {
    //    //    return ReturnResult.buildFailureResult("id 为空 ", false);
    //    } catch (Exception e) {
    //        return ReturnResult.buildFailureResult("参数错误");
    //    }
    //}

    //@PostMapping("/upload")
    //@ApiOperation("Excel导入数据")
    //public ReturnResult excelToDatabase(@ApiParam(value = "上传文件", type = "_file") @RequestPart("file") MultipartFile file) {
    //    try {
    //        if (!file.isEmpty()) {
    //            if (flowsServiceTest.importFileToDB(true, file)) {
    //                return ReturnResult.buildSuccessResult("导入成功", null);
    //            } else {
    //                return ReturnResult.buildFailureResult("导入失败", null);
    //            }
    //        } else {
    //            return ReturnResult.buildFailureResult("文件为空", null);
    //        }
    //    }
    //    catch (Exception e){
    //        return ReturnResult.buildFailureResult("不支持此文件类型 或 文件内容格式不对", null);
    //    }
    //}
}
